﻿using Kaskelot.SP.Core;
using Kaskelot.SP.Model.Contracts;
using Kaskelot.SP.Services;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

namespace Kaskelot.SP.UI.CONTROLTEMPLATES.Kaskelot.SP.UI.Forms
{
    public partial class PersonForm : BaseForm, IForm
    {
        public string Id { get; set; }
        public string ParentId { get; set; }
        public Person Current { get; set; }

        protected string GetSelected(object value, PersonTyp[] sourceValues)
        {
            if (sourceValues == null)
                return "";

            if(sourceValues.Any(pt => pt.Id.ToString().Equals(value.ToString())))
                return "selected=\"selected\"";

            return "";
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Id))
                {
                    PersonService svc = new PersonService();

                    Guid personId = new Guid(this.Id);
                    this.Current = svc.GetPerson(personId);
                }
                else
                {
                    this.Current = new Person();
                }
                using (LookupService lsvc = new LookupService())
                {
                    PersonSearchLookupResult pslr = lsvc.GetPersonSearchLookupData();
                    IList<ListItem> typer = new List<ListItem>();
                    foreach (PersonTyp typ in pslr.PersonTyper)
                    {
                        typer.Add(new ListItem() { Text = typ.Namn, Value = typ.Id.ToString() });
                    }
                    TypRepeater.DataSource = typer;
                    TypRepeater.DataBind();
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(this, LayerNames.UI, "Page_Load", "Failed to load person form", ex);
            }
        }

        
    }
}
